<?php
 /**
 * ResMania
 * More Information contact: info@resmania.com
 *
 * @package Joomla - ResMania Router/Route Class
 * @version 0.5
 * @author webformatique
 * @copyright (C) 2010- webformatique
 * @license GNU/GPLv3 http://www.gnu.org/copyleft/gpl.html
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

class RM_Joomla_Controller_Router_Route implements RM_Controller_Router_Route_Interface {
    /**
     * Controller name
     *
     * @var string
     */
    protected $_controller = "";

    /**
     * Action name
     *
     * @var string
     */
    protected $_action = "";

    /**
     * Module name
     *
     * @var string
     */
    protected $_module = "";

    /**
     * Instantiates route based on passed Zend_Config structure
     *
     * @param Zend_Config $config Configuration object
     */
    public static function getInstance(Zend_Config $config)
    {
        $defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
        return new self($config->route, $defs);
    }

    /**
     * Prepares the route for mapping.
     *
     * @param string $route Map used to match with later submitted URL path
     * @param array $defaults Defaults for map variables with keys as variable names
     */
    public function __construct($defaults = array())
    {
        $this->_controller = $defaults['controller'];
        $this->_action = $defaults['action'];
        $this->_module = $defaults['module'];
    }

    /**
     * Matches a user submitted path with a previously defined route.
     * Assigns and returns an array of defaults on a successful match.
     *
     * @param string $path Path used to match against this routing map
     * @return array|false An array of assigned values or a false on a mismatch
     */
    public function match($path)
    {
        $chunks = explode('/', trim($path, '?'));
        $url = $chunks[count($chunks) - 1];
	    $url = trim($url, '?');

        parse_str($url, $return);

        unset($return['option']);
        $return['controller'] = isset($return['act']) ? $return['act'] : $this->_controller;
        $return['action'] = isset($return['task']) ? $return['task'] : $this->_action;
        $return['module'] = $this->_module;

        unset($return['act']);
        unset($return['task']);

        $this->_controller = $return['controller'];
        $this->_action = $return['action'];

        return $return;
    }

    /**
     * Assembles a URL path defined by this route
     *
     * @param array $data An array of variable and value pairs used as parameters
     * @return string Route path with user submitted parameters
     */
    public function assemble($data = array(), $reset = false, $encode = false)
    {
        $url = '?option=com_resmania';
        if (count($data) > 0) {
            $url .= "&".http_build_query($data,'','&');
        }
        return $url;
    }

    /**
     * Returns an js code of function that assembles a URL path
     *
     * @todo typo in method name is terrible...
     * @return string JS function named 'RM.Common.AssembleURL' for assemble
     */
    public function getAccembleURLJsCode()
    {
        if (RM_Joomla_Connector::is16()) {
            return $this->_getAssembledURLJsCode16();
        }
        if (RM_Joomla_Connector::is15()) {
            return $this->_getAssembledURLJsCode15();
        }
        return null;
    }

    private function _getAssembledURLJsCode15(){
        if ($this->_module == 'admin') {
            $index = 'index3.php';
        } else {
            $index = 'index.php';
        }
        return "/**
            * This code is created by the Joomla router class
            *
            * @usage    json.js is boolean and if passed as true will cause the
            *           interface to render without a wrapper.
            *           json.parameter is the parameters to pass
            * @params   string    json string containing params
            */
            RM.Common.AssembleURL = function(json){
            if (json.forcefrontend === true){
                var url = 'index2.php?option=com_resmania&act='+json.controller+'&task='+json.action;
            } else {
                var url = '".$index."?option=com_resmania&act='+json.controller+'&task='+json.action;
            }
            if (json.parameters) {
                for (var i = 0; i < json.parameters.length; i++){
                    url = url + '&' + json.parameters[i].name + '=' + json.parameters[i].value;
                }
            }
            return url;
        }";
    }

    private function _getAssembledURLJsCode16(){
        return "/**
            * This code is created by the Joomla router class
            * 
            * @usage    json.js is boolean and if passed as true will cause the
            *           interface to render without a wrapper.
            *           json.parameter is the parameters to pass
            * @params   string    json string containing params
            */
            RM.Common.AssembleURL = function(json){
            var url = 'index.php?option=com_resmania&act=' + json.controller + '&task=' + json.action;
            if (json.js){
                url = url + '&format=row';
            }
            if (json.parameters) {
                for (var i = 0; i < json.parameters.length; i++){
                    url = url + '&' + json.parameters[i].name + '=' + json.parameters[i].value;
                }
            }
            return url;
        }";
    }
}